﻿namespace FiftyOne.Foundation.Mobile.Detection.Wurfl
{
    using FiftyOne.Foundation.Image;
    using FiftyOne.Foundation.Mobile.Detection;
    using System;
    using System.Collections;
    using System.Globalization;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.Mobile;

    public class MobileCapabilities : FiftyOne.Foundation.Mobile.Detection.MobileCapabilities
    {
        public MobileCapabilities(IDictionary capabilities) : base(capabilities)
        {
        }

        public MobileCapabilities(string userAgent) : base(userAgent)
        {
            this.Init(Provider.Instance.GetDeviceInfo(userAgent));
        }

        public MobileCapabilities(HttpContext context) : base(context)
        {
            this.Init(Provider.Instance.GetDeviceInfo(context));
        }

        private static void Enhance(IDictionary capabilities, DeviceInfo device)
        {
            SetStaticValues(capabilities);
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "deviceid", device.DeviceId);
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "actualDeviceRoot", device.IsActualDeviceRoot.ToString());
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "isMobileDevice", GetIsMobileDevice(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "crawler", GetIsCrawler(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "mobileDeviceModel", GetMobileDeviceModel(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "mobileDeviceManufacturer", GetMobileDeviceManufacturer(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "platform", GetPlatform(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "type", (string) capabilities["mobileDeviceManufacturer"]);
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "supportsAccessKeyAttribute", GetSupportsAccesskeyAttribute(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "hasBackButton", GetHasBackButton(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "screenPixelsHeight", GetScreenPixelsHeight(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "screenPixelsWidth", GetScreenPixelsWidth(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "screenBitDepth", GetScreenBitDepth(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "physicalScreenWidth", GetPhysicalScreenWidth(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "physicalScreenHeight", GetPhysicalScreenHeight(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "maxImagePixelsWidth", GetMaxImageWidth(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "maxImagePixelsHeight", GetMaxImageHeight(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "preferredImageMime", GetPreferredImageMime(device, capabilities));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "isColor", GetIsColor(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "pointingMethod", GetPointingMethod(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "dualOrientation", GetDualOrientation(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "majorversion", GetMajorVersion(device, (string) capabilities["majorversion"]));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "minorversion", GetMinorVersion(device, (string) capabilities["minorversion"]));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "version", ((string) capabilities["majorversion"]) + ((string) capabilities["minorversion"]));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "supportsImageSubmit", GetSupportsImageSubmit(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "viewport_supported", GetMetaViewPortSupported(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "mobileoptimized", GetMetaMobileOptimizedSupported(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "handheldfriendly", GetMetaHandHeldFriendlySupported(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "css_supports_width_as_percentage", GetCssSupportsWidthAsPercentage(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "css_spriting", GetCssSpriting(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "css_gradient", GetCssSupportGroup(device, "css_gradient"));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "css_border_image", GetCssSupportGroup(device, "css_border_image"));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "css_rounded_corners", GetCssSupportGroup(device, "css_rounded_corners"));
            bool javaScript = false;
            javaScript = GetJavascriptSupport(device, (string) capabilities["javascript"]);
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetJavaScript(capabilities, javaScript);
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "ecmascriptversion", javaScript ? "3.0.0.0" : "0.0.0.0");
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "jscriptversion", javaScript ? "5.7.0.0" : "0.0.0.0");
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "cssManipulation", GetCssManipulationSupport(device));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "w3cdomversion", GetW3CDOMVersion(device, (string) capabilities["w3cdomversion"]));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "cookies", GetCookieSupport(device, (string) capabilities["cookies"]));
            SetAudio(capabilities, device);
            string preferredRenderingTypeFromWURFL = GetPreferredRenderingTypeFromWURFL(device);
            if (!string.IsNullOrEmpty(preferredRenderingTypeFromWURFL))
            {
                FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "preferredRenderingType", preferredRenderingTypeFromWURFL);
            }
            string preferredRenderingMimeFromWURFL = GetPreferredRenderingMimeFromWURFL(device, preferredRenderingTypeFromWURFL);
            if (!string.IsNullOrEmpty(preferredRenderingMimeFromWURFL))
            {
                FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "preferredRenderingMime", preferredRenderingMimeFromWURFL);
            }
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "supportsCss", GetCssSupport(preferredRenderingTypeFromWURFL));
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "tables", GetTables(device, preferredRenderingTypeFromWURFL));
        }

        private static string GetAudioFormatSupport(DeviceInfo device, string format)
        {
            string capability = device.GetCapability(format);
            if (bool.TrueString.Equals(capability, StringComparison.InvariantCultureIgnoreCase) || bool.FalseString.Equals(capability, StringComparison.InvariantCultureIgnoreCase))
            {
                return capability;
            }
            return bool.FalseString.ToLowerInvariant();
        }

        private static string GetCookieSupport(DeviceInfo device, string current)
        {
            bool result = false;
            if (!bool.TryParse(device.GetCapability("cookie_support"), out result))
            {
                bool.TryParse(current, out result);
            }
            return result.ToString();
        }

        private static string GetCssManipulationSupport(DeviceInfo device)
        {
            bool result = false;
            if (bool.TryParse(device.GetCapability("ajax_manipulate_css"), out result) && result)
            {
                return bool.TrueString.ToLowerInvariant();
            }
            return bool.FalseString.ToLowerInvariant();
        }

        private static bool GetCssSpriting(DeviceInfo deviceInfo)
        {
            bool capabilityValue = false;
            deviceInfo.GetCapability("css_spriting", out capabilityValue);
            return capabilityValue;
        }

        private static string GetCssSupport(string renderingType)
        {
            if (renderingType.Contains("html"))
            {
                return bool.TrueString.ToLowerInvariant();
            }
            return bool.FalseString.ToLowerInvariant();
        }

        private static string GetCssSupportGroup(DeviceInfo device, string capabilityName)
        {
            string capability = device.GetCapability(capabilityName);
            if (CssGroup.CSS3.ToString().Equals(capability, StringComparison.InvariantCultureIgnoreCase))
            {
                return CssGroup.CSS3.ToString();
            }
            if (CssGroup.Mozilla.ToString().Equals(capability, StringComparison.InvariantCultureIgnoreCase))
            {
                return CssGroup.Mozilla.ToString();
            }
            if (CssGroup.Opera.ToString().Equals(capability, StringComparison.InvariantCultureIgnoreCase))
            {
                return CssGroup.Opera.ToString();
            }
            if (CssGroup.WebKit.ToString().Equals(capability, StringComparison.InvariantCultureIgnoreCase))
            {
                return CssGroup.WebKit.ToString();
            }
            return CssGroup.None.ToString();
        }

        private static bool GetCssSupportsWidthAsPercentage(DeviceInfo deviceInfo)
        {
            bool capabilityValue = false;
            deviceInfo.GetCapability("css_supports_width_as_percentage", out capabilityValue);
            return capabilityValue;
        }

        private static string GetDualOrientation(DeviceInfo device)
        {
            if (bool.TrueString.Equals(device.GetCapability("dual_orientation"), StringComparison.InvariantCultureIgnoreCase))
            {
                return bool.TrueString.ToLowerInvariant();
            }
            return bool.FalseString.ToLowerInvariant();
        }

        private static string GetFirstRenderingTypeFromWURFL(DeviceInfo device)
        {
            bool flag;
            if (device.GetCapability("html_wi_oma_xhtmlmp_1_0", out flag) && flag)
            {
                return "xhtml-mp";
            }
            if (device.GetCapability("html_wi_w3_xhtmlbasic", out flag) && flag)
            {
                return "xhtml-basic";
            }
            if (device.GetCapability("html_wi_imode_compact_generic", out flag) && flag)
            {
                return System.Web.Mobile.MobileCapabilities.PreferredRenderingTypeChtml10;
            }
            if (device.GetCapability("html_web_4_0", out flag) && flag)
            {
                return System.Web.Mobile.MobileCapabilities.PreferredRenderingTypeHtml32;
            }
            if (device.GetCapability("html_web_3_2", out flag) && flag)
            {
                return System.Web.Mobile.MobileCapabilities.PreferredRenderingTypeHtml32;
            }
            return null;
        }

        private static string GetHasBackButton(DeviceInfo device)
        {
            return device.GetCapability("built_in_back_button_support");
        }

        private static string GetIsColor(DeviceInfo device)
        {
            long capabilityValue = 0L;
            device.GetCapability("colors", out capabilityValue);
            bool flag = capabilityValue >= 0x100L;
            return flag.ToString(CultureInfo.InvariantCulture);
        }

        private static string GetIsCrawler(DeviceInfo device)
        {
            if ((device.DeviceId == "generic_web_crawler") || device.DeviceId.Contains("crawler"))
            {
                return bool.TrueString.ToLowerInvariant();
            }
            if (device.FallbackDevice != null)
            {
                return GetIsCrawler(device.FallbackDevice);
            }
            return bool.FalseString.ToLowerInvariant();
        }

        private static string GetIsMobileDevice(DeviceInfo device)
        {
            bool result = false;
            if (bool.TryParse(device.GetCapability("is_wireless_device"), out result) && result)
            {
                return bool.TrueString.ToLowerInvariant();
            }
            return bool.FalseString.ToLowerInvariant();
        }

        private static bool GetJavascriptSupport(DeviceInfo device, string current)
        {
            bool result = false;
            return (((bool.TryParse(current, out result) && result) && bool.TryParse(device.GetCapability("ajax_support_javascript"), out result)) && result);
        }

        private static string GetMajorVersion(string value)
        {
            MatchCollection matchs = SegmentVersionString(value);
            if ((matchs != null) && (matchs.Count >= 1))
            {
                return matchs[0].Value;
            }
            return null;
        }

        private static string GetMajorVersion(DeviceInfo device, string current)
        {
            decimal result = 0M;
            if (!decimal.TryParse(GetMajorVersion(device.GetCapability("mobile_browser_version")), out result))
            {
                decimal.TryParse(current, out result);
            }
            return result.ToString();
        }

        private static string GetMaxImageHeight(DeviceInfo deviceInfo)
        {
            int capabilityValue = 0;
            deviceInfo.GetCapability("max_image_height", out capabilityValue);
            return capabilityValue.ToString();
        }

        private static string GetMaxImageWidth(DeviceInfo deviceInfo)
        {
            int capabilityValue = 0;
            deviceInfo.GetCapability("max_image_width", out capabilityValue);
            return capabilityValue.ToString();
        }

        private static bool GetMetaHandHeldFriendlySupported(DeviceInfo deviceInfo)
        {
            bool capabilityValue = false;
            deviceInfo.GetCapability("handheldfriendly", out capabilityValue);
            return capabilityValue;
        }

        private static bool GetMetaMobileOptimizedSupported(DeviceInfo deviceInfo)
        {
            bool capabilityValue = false;
            deviceInfo.GetCapability("mobileoptimized", out capabilityValue);
            return capabilityValue;
        }

        private static bool GetMetaViewPortSupported(DeviceInfo deviceInfo)
        {
            bool capabilityValue = false;
            deviceInfo.GetCapability("viewport_supported", out capabilityValue);
            return capabilityValue;
        }

        private static string GetMinorVersion(string value)
        {
            MatchCollection matchs = SegmentVersionString(value);
            if ((matchs != null) && (matchs.Count > 1))
            {
                StringBuilder builder = new StringBuilder();
                for (int i = 1; i < matchs.Count; i++)
                {
                    builder.Append(matchs[i].Value);
                    if (i < (matchs.Count - 1))
                    {
                        builder.Append(".");
                    }
                }
                return builder.ToString();
            }
            return null;
        }

        private static string GetMinorVersion(DeviceInfo device, string current)
        {
            decimal result = 0M;
            if (!decimal.TryParse(GetMinorVersion(device.GetCapability("mobile_browser_version")), out result))
            {
                decimal.TryParse(current, out result);
            }
            return result.ToString(".0");
        }

        private static string GetMobileDeviceManufacturer(DeviceInfo device)
        {
            return device.GetCapability("brand_name");
        }

        private static string GetMobileDeviceModel(DeviceInfo device)
        {
            string capability = device.GetCapability("marketing_name");
            if (string.IsNullOrEmpty(capability))
            {
                capability = device.GetCapability("model_name");
            }
            return capability;
        }

        private static string GetPhysicalScreenHeight(DeviceInfo deviceInfo)
        {
            int capabilityValue = 0;
            deviceInfo.GetCapability("physical_screen_height", out capabilityValue);
            return capabilityValue.ToString();
        }

        private static string GetPhysicalScreenWidth(DeviceInfo deviceInfo)
        {
            int capabilityValue = 0;
            deviceInfo.GetCapability("physical_screen_width", out capabilityValue);
            return capabilityValue.ToString();
        }

        private static string GetPlatform(DeviceInfo device)
        {
            return device.GetCapability("device_os");
        }

        private static string GetPointingMethod(DeviceInfo device)
        {
            string capability = device.GetCapability("pointing_method");
            if (PointingMethods.Clickwheel.ToString().Equals(capability, StringComparison.InvariantCultureIgnoreCase))
            {
                return PointingMethods.Clickwheel.ToString();
            }
            if (PointingMethods.Joystick.ToString().Equals(capability, StringComparison.InvariantCultureIgnoreCase))
            {
                return PointingMethods.Joystick.ToString();
            }
            if (PointingMethods.Stylus.ToString().Equals(capability, StringComparison.InvariantCultureIgnoreCase))
            {
                return PointingMethods.Stylus.ToString();
            }
            if (PointingMethods.Touchscreen.ToString().Equals(capability, StringComparison.InvariantCultureIgnoreCase))
            {
                return PointingMethods.Touchscreen.ToString();
            }
            return PointingMethods.Unknown.ToString();
        }

        private static string GetPreferredImageMime(DeviceInfo device, IDictionary capabilities)
        {
            if (bool.TrueString.Equals(device.GetCapability("png"), StringComparison.InvariantCultureIgnoreCase))
            {
                return "image/png";
            }
            if (bool.TrueString.Equals(device.GetCapability("jpg"), StringComparison.InvariantCultureIgnoreCase))
            {
                return "image/jpeg";
            }
            if (bool.TrueString.Equals(device.GetCapability("gif"), StringComparison.InvariantCultureIgnoreCase))
            {
                return "image/gif";
            }
            return null;
        }

        private static string GetPreferredRenderingMimeFromWURFL(DeviceInfo device, string renderingType)
        {
            switch (renderingType)
            {
                case "xhtml-mp":
                case "xhtml-basic":
                    return device.GetCapability("xhtmlmp_preferred_mime_type");

                case "chtml10":
                case "html4":
                case "html32":
                    return "text/html";
            }
            return null;
        }

        private static string GetPreferredRenderingTypeFromWURFL(DeviceInfo device)
        {
            switch (device.GetCapability("preferred_markup"))
            {
                case "html_web_3_2":
                    return System.Web.Mobile.MobileCapabilities.PreferredRenderingTypeHtml32;

                case "html_web_4_0":
                    return "html4";

                case "html_wi_oma_xhtmlmp_1_0":
                    return "xhtml-mp";

                case "html_wi_w3_xhtmlbasic":
                    return "xhtml-basic";

                case "html_wi_imode_htmlx_1":
                case "html_wi_imode_html_1":
                case "html_wi_imode_html_2":
                case "html_wi_imode_html_3":
                case "html_wi_imode_html_4":
                case "html_wi_imode_html_5":
                case "html_wi_imode_html_6":
                case "html_wi_imode_htmlx_1_1":
                case "html_wi_imode_compact_generic":
                    return System.Web.Mobile.MobileCapabilities.PreferredRenderingTypeChtml10;
            }
            return GetFirstRenderingTypeFromWURFL(device);
        }

        private static string GetScreenBitDepth(DeviceInfo device)
        {
            long capabilityValue = 0x100L;
            device.GetCapability("colors", out capabilityValue);
            return ((long) Support.GetBitsPerPixel(capabilityValue)).ToString(CultureInfo.InvariantCulture);
        }

        private static string GetScreenPixelsHeight(DeviceInfo device)
        {
            return device.GetCapability("resolution_height");
        }

        private static string GetScreenPixelsWidth(DeviceInfo device)
        {
            return device.GetCapability("resolution_width");
        }

        private static string GetSupportsAccesskeyAttribute(DeviceInfo device)
        {
            return device.GetCapability("access_key_support");
        }

        private static string GetSupportsImageSubmit(DeviceInfo device)
        {
            int result = 0;
            if (int.TryParse(device.GetCapability("xhtml_support_level"), out result) && (result >= 3))
            {
                return bool.TrueString.ToLowerInvariant();
            }
            return bool.FalseString.ToLowerInvariant();
        }

        private static string GetTables(DeviceInfo device, string renderType)
        {
            bool result = false;
            string capability = null;
            if (renderType.Contains("chtml"))
            {
                capability = device.GetCapability("chtml_table_support");
            }
            else if (renderType.Contains("xhtml"))
            {
                capability = device.GetCapability("xhtml_table_support");
            }
            else if (renderType.Contains("wml"))
            {
                capability = device.GetCapability("table_support");
            }
            else if (renderType.Contains("html"))
            {
                capability = bool.TrueString;
            }
            else
            {
                capability = bool.FalseString;
            }
            if (bool.TryParse(capability, out result) && result)
            {
                return bool.TrueString.ToLowerInvariant();
            }
            return bool.FalseString.ToLowerInvariant();
        }

        private static string GetW3CDOMVersion(DeviceInfo device, string current)
        {
            int result = 0;
            Version version = new Version(0, 0);
            if (int.TryParse(device.GetCapability("xhtml_support_level"), out result) && (result >= 4))
            {
                version = new Version("1.0.0.0");
            }
            else
            {
                try
                {
                    version = new Version(current);
                }
                catch (ArgumentNullException)
                {
                }
            }
            return version.ToString();
        }

        protected void Init(DeviceInfo device)
        {
            if (device != null)
            {
                Enhance(base.Capabilities, device);
            }
            base.Init();
        }

        internal override bool IsAudioFormatSupported(AudioFormats format)
        {
            switch (format)
            {
                case AudioFormats.Rmf:
                    return bool.TrueString.Equals(this["rmf"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Qcelp:
                    return bool.TrueString.Equals(this["qcelp"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Awb:
                    return bool.TrueString.Equals(this["awb"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Smf:
                    return bool.TrueString.Equals(this["smf"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Wav:
                    return bool.TrueString.Equals(this["wav"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.NokiaRingtone:
                    return bool.TrueString.Equals(this["nokia_ringtone"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Aac:
                    return bool.TrueString.Equals(this["aac"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Digiplug:
                    return bool.TrueString.Equals(this["digiplug"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.SpMidi:
                    return bool.TrueString.Equals(this["sp_midi"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.CompactMidi:
                    return bool.TrueString.Equals(this["compactmidi"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Mp3:
                    return bool.TrueString.Equals(this["mp3"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Mld:
                    return bool.TrueString.Equals(this["mld"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Evrc:
                    return bool.TrueString.Equals(this["evrc"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Amr:
                    return bool.TrueString.Equals(this["amr"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Xmf:
                    return bool.TrueString.Equals(this["xmf"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Mmf:
                    return bool.TrueString.Equals(this["mmf"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.IMelody:
                    return bool.TrueString.Equals(this["imelody"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.MidiMonophonic:
                    return bool.TrueString.Equals(this["midi_monophonic"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.Au:
                    return bool.TrueString.Equals(this["au"], StringComparison.InvariantCultureIgnoreCase);

                case AudioFormats.MidiPolyphonic:
                    return bool.TrueString.Equals(this["midi_polyphonic"], StringComparison.InvariantCultureIgnoreCase);
            }
            return false;
        }

        private static MatchCollection SegmentVersionString(string value)
        {
            if (value != null)
            {
                return Regex.Matches(value, @"\d+");
            }
            return null;
        }

        private static void SetAudio(IDictionary capabilities, DeviceInfo device)
        {
            foreach (string str in FiftyOne.Foundation.Mobile.Detection.Wurfl.Constants.Audio_Formats)
            {
                FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, str, GetAudioFormatSupport(device, str));
            }
        }

        private static void SetStaticValues(IDictionary capabilities)
        {
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "requiresSpecialViewStateEncoding", "true");
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "requiresUniqueFilePathSuffix", "true");
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "requiresUniqueHtmlCheckboxNames", "true");
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "requiresUniqueHtmlInputNames", "true");
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "requiresUrlEncodedPostfieldValues", "true");
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "requiresOutputOptimization", "true");
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "requiresControlStateInSession", "true");
            if (capabilities["majorversion"] == null)
            {
                FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "majorversion", "1");
            }
            if (capabilities["minorversion"] == null)
            {
                FiftyOne.Foundation.Mobile.Detection.MobileCapabilities.SetValue(capabilities, "minorversion", "0");
            }
        }

        public bool ActualDeviceRoot
        {
            get
            {
                bool result = false;
                bool.TryParse(this["actualDeviceRoot"], out result);
                return result;
            }
        }

        public override CssGroup CssBorderImageSupport
        {
            get
            {
                string str = base.Capabilities["css_border_image"] as string;
                if (CssGroup.CSS3.ToString().Equals(str))
                {
                    return CssGroup.CSS3;
                }
                if (CssGroup.Mozilla.ToString().Equals(str))
                {
                    return CssGroup.Mozilla;
                }
                if (CssGroup.Opera.ToString().Equals(str))
                {
                    return CssGroup.Opera;
                }
                if (CssGroup.WebKit.ToString().Equals(str))
                {
                    return CssGroup.WebKit;
                }
                return CssGroup.None;
            }
        }

        public override CssGroup CssGradientSupport
        {
            get
            {
                string str = base.Capabilities["css_gradient"] as string;
                if (CssGroup.CSS3.ToString().Equals(str))
                {
                    return CssGroup.CSS3;
                }
                if (CssGroup.Mozilla.ToString().Equals(str))
                {
                    return CssGroup.Mozilla;
                }
                if (CssGroup.Opera.ToString().Equals(str))
                {
                    return CssGroup.Opera;
                }
                if (CssGroup.WebKit.ToString().Equals(str))
                {
                    return CssGroup.WebKit;
                }
                return CssGroup.None;
            }
        }

        public override bool CssManipulation
        {
            get
            {
                bool result = false;
                return (bool.TryParse(base.Capabilities["cssManipulation"] as string, out result) && result);
            }
        }

        public override CssGroup CssRoundedCornerSupport
        {
            get
            {
                string str = base.Capabilities["css_rounded_corners"] as string;
                if (CssGroup.CSS3.ToString().Equals(str))
                {
                    return CssGroup.CSS3;
                }
                if (CssGroup.Mozilla.ToString().Equals(str))
                {
                    return CssGroup.Mozilla;
                }
                if (CssGroup.Opera.ToString().Equals(str))
                {
                    return CssGroup.Opera;
                }
                if (CssGroup.WebKit.ToString().Equals(str))
                {
                    return CssGroup.WebKit;
                }
                return CssGroup.None;
            }
        }

        public override bool CssSpriting
        {
            get
            {
                bool result = false;
                bool.TryParse(base.Capabilities["css_spriting"].ToString(), out result);
                return result;
            }
        }

        public override bool CssSupportsWidthAsPercentage
        {
            get
            {
                bool result = false;
                bool.TryParse(base.Capabilities["css_supports_width_as_percentage"].ToString(), out result);
                return result;
            }
        }

        public string DeviceId
        {
            get
            {
                return this["deviceid"];
            }
        }

        public override FiftyOne.Foundation.Mobile.Detection.DualOrientation DualOrientation
        {
            get
            {
                object obj2 = base.Capabilities["dualOrientation"];
                if (obj2 is FiftyOne.Foundation.Mobile.Detection.DualOrientation)
                {
                    return (FiftyOne.Foundation.Mobile.Detection.DualOrientation) obj2;
                }
                return FiftyOne.Foundation.Mobile.Detection.DualOrientation.Unknown;
            }
        }

        public override int MaxImagePixelsHeight
        {
            get
            {
                int result = 0;
                if (int.TryParse(base.Capabilities["maxImagePixelsHeight"] as string, out result))
                {
                    return result;
                }
                return this.ScreenPixelsWidth;
            }
        }

        public override int MaxImagePixelsWidth
        {
            get
            {
                int result = 0;
                if (int.TryParse(base.Capabilities["maxImagePixelsWidth"] as string, out result))
                {
                    return result;
                }
                return this.ScreenPixelsWidth;
            }
        }

        public override bool MetaHandHeldFriendlySupported
        {
            get
            {
                bool result = false;
                bool.TryParse(base.Capabilities["handheldfriendly"].ToString(), out result);
                return result;
            }
        }

        public override bool MetaMobileOptimizedSupported
        {
            get
            {
                bool result = false;
                bool.TryParse(base.Capabilities["mobileoptimized"].ToString(), out result);
                return result;
            }
        }

        public override bool MetaViewportSupported
        {
            get
            {
                bool result = false;
                bool.TryParse(base.Capabilities["viewport_supported"].ToString(), out result);
                return result;
            }
        }

        public override int PhysicalScreenHeight
        {
            get
            {
                int num;
                int.TryParse(base.Capabilities["physicalScreenHeight"] as string, out num);
                return num;
            }
        }

        public override int PhysicalScreenWidth
        {
            get
            {
                int num;
                int.TryParse(base.Capabilities["physicalScreenWidth"] as string, out num);
                return num;
            }
        }

        public override PointingMethods PointingMethod
        {
            get
            {
                string str = this["pointingMethod"];
                if (PointingMethods.Clickwheel.ToString().Equals(str))
                {
                    return PointingMethods.Clickwheel;
                }
                if (PointingMethods.Joystick.ToString().Equals(str))
                {
                    return PointingMethods.Joystick;
                }
                if (PointingMethods.Stylus.ToString().Equals(str))
                {
                    return PointingMethods.Stylus;
                }
                if (PointingMethods.Touchscreen.ToString().Equals(str))
                {
                    return PointingMethods.Touchscreen;
                }
                return PointingMethods.Unknown;
            }
        }
    }
}

